package com.easyui.controller;

import com.easyui.pojo.Menu;
import com.easyui.pojo.User;
import com.easyui.service.MenuService;
import com.easyui.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user.html")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @RequestMapping(params = "act=page")
    public String page() {
        return "/user_list";
    }

    @RequestMapping(params = "act=go_edit")
    public String goEdit(Integer id, Model model) {
        if (id != null) {
            User user = userService.getUserById(id);
            model.addAttribute("user", user);
        }
        return "/user_add";
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public Map<String, Object> list(User user, int page, int rows) {
        PageInfo<User> pageInfo = userService.getUserList(user, page, rows);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String, Object> edit(User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (user.getId() == null) {
                userService.addUser(user);
            } else {
                userService.updateUser(user);
            }
            result.put("success", true);
            result.put("message", "编辑成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(Integer[] ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            userService.deleteUser(ids);
            result.put("success", true);
            result.put("message", "删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(params = "act=assign_menu")
    @ResponseBody
    public Map<String, Object> assignMenu(Integer[] menuIds, Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            menuService.addUserMenu(menuIds, userId);
            result.put("success", true);
            result.put("message", "授权成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(params = "act=menu_tree")
    @ResponseBody
    public List<Menu> menuTree() {
        return menuService.getTreeMenu();
    }

    @RequestMapping(params = "act=go_assign_menu")
    public String goAssignMenu() {
        return "/assign_user_menu";
    }

    @RequestMapping(params = "act=user_menu")
    @ResponseBody
    public List<Integer> userMenu(Integer userId) {
        return menuService.getUserMenuIds(userId);
    }
}
